package com.logic.landseaserver.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.logic.common.enums.LogTypeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.common.util.SysUtil;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.domain.CleanLog;
import com.logic.landseaserver.domain.OperationLog;
import com.logic.landseaserver.persistence.read.CleanLogReadMapper;
import com.logic.landseaserver.persistence.read.OperationLogReadMapper;
import com.logic.landseaserver.persistence.write.CleanLogWriteMapper;
import com.logic.landseaserver.persistence.write.OperationLogHistoryWriteMapper;
import com.logic.landseaserver.persistence.write.OperationLogWriteMapper;
import com.logic.landseaserver.service.IOperationLogService;
import com.logic.landseaserver.ws.dto.CleanLogDTO;
import com.logic.landseaserver.ws.dto.OperationLogDTO;

@Service
public class OpertationLogService implements IOperationLogService {

    @Autowired
    private OperationLogReadMapper logReadMapper;

    @Autowired
    private OperationLogWriteMapper operationlogWriteMapper;
    
    @Autowired
    private OperationLogHistoryWriteMapper operationLogHistoryWriteMapper;
    
    
    @Autowired
    private  CleanLogWriteMapper cleanLogWriteMapper;
    
    @Autowired
    private  CleanLogReadMapper cleanLogReadMapper;

    @Override
    public void log(String ip, Integer createdBy, String message, String type, String moduleType, Integer moduleId) {
        OperationLog log = new OperationLog();
        log.setIp(ip);
        log.setCreatedBy(createdBy);
        log.setType(type);
        log.setModuleType(moduleType);
        log.setModuleId(moduleId);
        log.setMessage(message);
        log.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
        operationlogWriteMapper.insertSelective(log);
    }

    @Override
    public void insert(OperationLog log) throws LandseaBusinessException {
        if (log.getModuleId() == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_SIGN_ID_IS_NULL);
        }

        if (log.getModuleType() == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_SIGN_ID_IS_NULL);
        }

        if (StringUtils.isEmpty(log.getMessage())) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_SIGN_ID_IS_NULL);
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        log(SysUtil.getClientIpAddr(request),
                SecurityUtils.getCurrentLogin().getUserId(), log.getMessage(),
                LogTypeEnum.EDIT.name(), log.getModuleType(), log.getModuleId());
    }

    @Override
    public List<OperationLogDTO> query(QueryUtil util) {
        return logReadMapper.query(util);
    }

    @Override
    public void cleanLog()throws BusinessException
    {
        List<OperationLog> operationLogs = logReadMapper.selectCleanLog();
        if (!operationLogs.isEmpty())
        {
            operationLogHistoryWriteMapper.cleanLog();
            for (OperationLog operationLog : operationLogs)
            {
                operationlogWriteMapper.deleteByPrimaryKey(operationLog.getId());
            }
            
        }
        else {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.OperationLog_Not_Exist);
        }
        
    }

    @Override
    public void saveCleanLog(CleanLog log)
    {
        log.setType(1);
        log.setMessage("操作");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        log.setIp(SysUtil.getClientIpAddr(request));
        log.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
        cleanLogWriteMapper.insertSelective(log);
        
    }

    @Override
    public List<CleanLogDTO> selectCleanLog()
    {
        List<CleanLogDTO> cleanLogDTOs = cleanLogReadMapper.selectCleanLog();
        return cleanLogDTOs;
    }

}
